(use-package :leam)
(clc:clc-require :flexi-streams)


(defun test-unsigned-8()
  (let ((data (flexi-streams:with-output-to-sequence (stream)
		(write-unsigned-8 stream 0)
		(write-unsigned-8 stream 255)
		(write-unsigned-8 stream 128))))
    (assert (= 3 (length data)))
    (flexi-streams:with-input-from-sequence (stream data)
      (assert (= 0 (read-unsigned-8 stream)))
      (assert (= 255 (read-unsigned-8 stream)))
      (assert (= 128 (read-unsigned-8 stream))))))

(defun test-unsigned-16()
  (let ((data (flexi-streams:with-output-to-sequence (stream)
		(write-unsigned-16 stream 0)
		(write-unsigned-16 stream 155)
		(write-unsigned-16 stream 65535)
		(write-unsigned-16 stream 32))))
    (assert (= 8 (length data)))
    (flexi-streams:with-input-from-sequence(stream data)
      (assert (= 0 (read-unsigned-16 stream)))
      (assert (= 155 (read-unsigned-16 stream)))
      (assert (= 65535 (read-unsigned-16 stream)))
      (assert (= 32 (read-unsigned-16 stream))))))

(defun test-unsigned-32()
  (let ((data (flexi-streams:with-output-to-sequence (stream)
		(write-unsigned-32 stream 0)
		(write-unsigned-32 stream 155)
		(write-unsigned-32 stream 65535)
		(write-unsigned-32 stream 32))))
    (assert (= 16 (length data)))
    (flexi-streams:with-input-from-sequence(stream data)
      (assert (= 0 (read-unsigned-32 stream)))
      (assert (= 155 (read-unsigned-32 stream)))
      (assert (= 65535 (read-unsigned-32 stream)))
      (assert (= 32 (read-unsigned-32 stream))))))

(defun test-char-string()
    (let ((data (flexi-streams:with-output-to-sequence (stream)
		  (write-char-string stream "one")
		  (write-char-string stream "foo")
		  (write-char-string stream "pizzled")))
	  (test (make-array 0 :adjustable 1 :fill-pointer 0
			    :element-type 'standard-char)))
      (assert (= 13 (length data)))
      (flexi-streams:with-input-from-sequence(stream data)
	(adjust-array test 3 :fill-pointer 3)
	(read-char-string stream test)
	(assert (string= test "one"))
	(read-char-string stream test)
	(assert (string= test "foo"))
	(adjust-array test 7 :fill-pointer 7)
	(read-char-string stream test)
	(assert (string= test "pizzled")))))

(defun test-byte-data()
  (let ((data (flexi-streams:with-output-to-sequence (stream)
		(write-byte-data stream
				 (make-array 4 :initial-element 255
					     :element-type '(unsigned-byte 8)))))
	(test-vec (make-array 4 :initial-element 0
			      :fill-pointer 4
			      :element-type '(unsigned-byte 8))))
    (flexi-streams:with-input-from-sequence( stream data )
      (read-byte-data stream test-vec)
      (assert (= 4 (length test-vec)))
      (assert (= 255 (aref test-vec 0)))
      (assert (= 255 (aref test-vec 1)))
      (assert (= 255 (aref test-vec 2)))
      (assert (= 255 (aref test-vec 3))))))

(defun leam-io-stream-test-all()
  (test-unsigned-8)
  (test-unsigned-16)
  (test-unsigned-32)
  (test-char-string)
  (test-byte-data))